<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input v-model="productConfigModel" placeholder="请输入物料类别名称" clearable size="small" prefix-icon="el-icon-search" style="margin-bottom: 20px" />
        </div>
        <div class="head-container" style="overflow:auto;height:76vh">
          <el-tree :data="productConfigOptions" :props="defaultProps" :expand-on-click-node="false" :filter-node-method="filterNode" ref="tree" default-expand-all @node-click="handleNodeClick" />·
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="物料名称" prop="name">
            <el-input v-model="queryParams.name" placeholder="请输入物料名称" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <el-form-item label="物料编码" prop="code">
            <el-input v-model="queryParams.code" placeholder="请输入物料编码" clearable size="small" @keyup.enter.native="handleQuery" />
          </el-form-item>
          <!--<el-form-item label="物料类别" prop="typeCode">
        <el-input
          v-model="queryParams.typeCode"
          placeholder="请输入物料类别"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择物料类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-download" size="mini" :disabled="multiple" @click="handleDownload" v-hasPermi="['basic:productInfo:download']">二维码下载</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="el-icon-download" size="mini" @click="handleRefresh" v-hasPermi="['basic:productInfo:refresh']">二维码批量刷新</el-button>
          </el-col>
          <!--<el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:productInfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:productInfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:productInfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:productInfo:export']"
        >导出</el-button>
      </el-col>-->
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="productInfoList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="编号" align="center" prop="id" />
          <el-table-column label="物料名称" align="center" prop="name" />
          <el-table-column label="物料编码" align="center" prop="code" />
          <el-table-column label="物料类别" align="center" prop="typeCodeName" />
          <!--<el-table-column label="物料类型" align="center" prop="typeName" />-->
          <el-table-column label="规格型号" align="center" prop="model" />
          <el-table-column label="单位" align="center" prop="unit" />
          <!--<el-table-column label="二维码信息" align="center" prop="picUrl" />
      <el-table-column label="显示顺序" align="center" prop="orderNum" />-->
          <el-table-column label="瓶型" align="center" prop="bottle" />
          <el-table-column label="所属主基地" align="center" prop="factoryName" />
          <el-table-column label="包装类型" align="center" prop="packType" :formatter="packTypeFormat" />
          <el-table-column label="每包数量" align="center" prop="number" />
          <el-table-column label="容量" align="center" prop="capacity" />
          <el-table-column label="是否标准产品" align="center" prop="isStandard" :formatter="isStandardFormat" />
          <!--<el-table-column label="预警值" align="center" prop="alarmNum" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="来源" align="center" prop="source" />-->
          <!--<el-table-column label="备注" align="center" prop="remark" />-->
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row)">查看</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['basic:productInfo:edit']">修改</el-button>
              <!-- <el-button
                 size="mini"
                 type="text"
                 icon="el-icon-delete"
                 @click="handleDelete(scope.row)"
                 v-hasPermi="['basic:productInfo:remove']"
               >删除</el-button> -->
            </template>
          </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
      </el-col>
    </el-row>

    <!-- 添加或修改物料信息管理对话框 -->
    <el-dialog v-myFullscreen v-loading="isLoading" element-loading-text="正在加载..." element-loading-spinner="el-icon-loading" element-loading-background="rgba(0, 0, 0, 0.8)" :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="物料名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入物料名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入物料编码" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类别" prop="typeCode">
              <!--<el-input v-model="form.typeCode" placeholder="请输入物料类别" :disabled="openDisabled" />-->
              <treeselect :searchable="false" v-model="form.typeCode" :options="productConfigOptions" :show-count="true" placeholder="请选择物料类别" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="物料分类" prop="type">
             <el-select v-model="form.type" placeholder="请选择物料分类" :disabled="openDisabled">
               <el-option v-for="dict in productTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
             </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规格型号" prop="model">
              <el-input v-model="form.model" placeholder="请输入规格型号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="单位" prop="unit">
              <el-input v-model="form.unit" placeholder="请输入单位" :disabled="openDisabled" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="瓶型" prop="bottle">
              <el-input v-model="form.bottle" placeholder="请输入瓶型" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属主基地" prop="factoryCode">
              <!--<el-input v-model="form.factoryName" placeholder="请输入所属主基地" :disabled="openDisabled" />-->
              <el-select v-model="form.factoryCode" placeholder="请选择所属主基地" :disabled="openDisabled">
                <el-option v-for="dict in factoryListOptions" :key="dict.deptId" :label="dict.name" :value="dict.deptId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="包装类型" prop="packType">
              <el-select v-model="form.packType" placeholder="请选择包装类型" :disabled="openDisabled">
                <el-option v-for="dict in packTypeOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每包数量" prop="number">
              <el-input v-model="form.number" placeholder="请输入每包数量" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="容量" prop="capacity">
              <el-input v-model="form.capacity" placeholder="请输入容量" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否标准产品" prop="isStandard">
              <!--<el-input v-model="form.isStandard" placeholder="请输入是否标准产品" :disabled="openDisabled" />-->
              <el-select v-model="form.isStandard" placeholder="请选择包装类型" :disabled="openDisabled">
                <el-option v-for="dict in isStandardOptions" :key="dict.dictValue" :label="dict.dictLabel" :value="dict.dictValue"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警值" prop="alarmNum">
              <el-input v-model="form.alarmNum" placeholder="请输入预警值" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否统计" prop="isStatistics">
              <!-- <el-input v-model="form.isStatistics" placeholder="请输入是否统计" :disabled="openDisabled" /> -->
              <el-select v-model="form.isStatistics" placeholder="请选择" :disabled="openDisabled">
                <el-option value="1" label="统计">统计</el-option>
                <el-option value="0" label="不统计">不统计</el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="价格" prop="price">
              <el-input v-model="form.price" placeholder="请输入价格" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <!--<el-col :span="12">
              <el-form-item label="显示顺序" prop="orderNum">
                <el-input v-model="form.orderNum" placeholder="请输入显示顺序" :disabled="openDisabled" />
              </el-form-item>
            </el-col>-->
          <el-col :span="12">
            <el-form-item label="来源" prop="source">
              <el-input v-model="form.source" placeholder="请输入来源" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
                <el-form-item label="状态">
                  <el-radio-group v-model="form.status" :disabled="openDisabled">
                    <el-radio label="1">请选择字典生成</el-radio>
                  </el-radio-group>
                </el-form-item>
            </el-col>-->
          <!--<el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
                </el-form-item>
            </el-col>-->
          <el-col :span="12">
            <el-form-item label="二维码信息" prop="picUrl">
              <!--<el-input v-model="form.picUrl" placeholder="请输入二维码信息" :disabled="openDisabled" />-->
              <img :src="form.picUrl" style="width: 150px" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProductInfo,
  getProductInfo,
  delProductInfo,
  addProductInfo,
  updateProductInfo,
  treeselect,
  downloadProductInfo,
  qrCodeRefresh,
  getFactoryList,
} from "@/api/basic/productInfo/productInfo";
import RightPanel from "../../../../components/RightPanel/index";
import Treeselect from "@riophae/vue-treeselect";
import { downLoadZip, downLoadZip2, resolveBlob2 } from "@/utils/zipdownload";

export default {
  name: "ProductInfo",
  components: { RightPanel, Treeselect },
  data() {
    return {
      //弹窗是否禁止编辑
      openDisabled: true,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料信息管理表格数据
      productInfoList: [],
      productConfigOptions: [],
      productTypeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 包装类型字典
      packTypeOptions: [],
      isStandardOptions: [],
      factoryListOptions: [],
      productConfigModel: undefined,
      defaultProps: {
        children: "children",
        label: "label",
      },
      isLoading: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        isStatistics: null,
        typeCode: null,
        type: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // type: [
        //   { required: true, message: "物料分类不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  watch: {
    // 根据名称筛选部门树
    productConfigModel(val) {
      this.$refs.tree.filter(val);
    },
  },
  created() {
    this.getList();
    this.getDicts("package_type").then((response) => {
      this.packTypeOptions = response.data;
    });
    this.getDicts("product_is_standard").then((response) => {
      this.isStandardOptions = response.data;
    });
    this.getDicts("product_report_type").then((response) => {
      this.productTypeOptions = response.data;
    });
    this.getTreeselect();
    this.getFactoryList();
  },
  methods: {
    /** 查询物料信息管理列表 */
    getList() {
      this.loading = true;
      listProductInfo(this.queryParams).then((response) => {
        this.productInfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    getFactoryList() {
      getFactoryList().then((response) => {
        this.factoryListOptions = response.data;
      });
    },
    /** 查询物料类别下拉树结构 */
    getTreeselect() {
      treeselect().then((response) => {
        this.productConfigOptions = response.data;
      });
    },
    // 筛选节点
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.typeCode = data.id;
      this.getList();
    },
    // 包装类型字典翻译
    packTypeFormat(row, column) {
      return this.selectDictLabel(this.packTypeOptions, row.packType);
    },

    isStandardFormat(row, column) {
      return this.selectDictLabel(this.isStandardOptions, row.isStandard);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        typeCode: null,
        type: null,
        model: null,
        unit: null,
        picUrl: null,
        orderNum: null,
        bottle: null,
        factoryCode: null,
        isStatistics: null,
        packType: null,
        number: null,
        capacity: null,
        isStandard: null,
        alarmNum: null,
        price: null,
        source: null,
        status: "0",
        delFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openDisabled = false;
      this.open = true;
      this.title = "添加物料信息管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.openDisabled = false;
      const id = row.id || this.ids;
      getProductInfo(id).then((response) => {
        this.form = response.data;
        if (this.form.factoryCode)  {
          this.form.factoryCode = Number(this.form.factoryCode);
        }else {
          this.form.factoryCode = null;
        }
        this.open = true;
        this.title = "修改物料信息管理";
      });
    },
    /** 查看按钮 */
    handleView(row) {
      this.reset();
      this.openDisabled = true;
      const id = row.id || this.ids;
      getProductInfo(id).then((response) => {
        this.form = response.data;
        this.form.factoryCode = Number(this.form.factoryCode);
        this.open = true;
        this.title = "查看物料信息管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.isLoading = true;
          if (this.form.id != null) {
            updateProductInfo(this.form)
              .then((response) => {
                this.isLoading = false;
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                this.isLoading = false;
              });
          } else {
            addProductInfo(this.form)
              .then((response) => {
                this.isLoading = false;
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              })
              .catch((error) => {
                this.isLoading = false;
              });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除物料信息管理编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delProductInfo(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 删除按钮操作 */
    handleDownload(row) {
      const ids = row.id || this.ids;
      /*downloadProductInfo(ids).then(response =>{
        resolveBlob2(response,'物料二维码');
      });*/
      downLoadZip2("basic/productInfo/download", ids, "newaaa");
    },

    handleRefresh() {
      qrCodeRefresh().then(() => {
        this.msgSuccess("二维码批量刷新成功");
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "basic/productInfo/export",
        {
          ...this.queryParams,
        },
        "物料信息" + this.parseTime(new Date()) + ".xlsx"
      );
    },
  },
};
</script>
